//
//  BVMineTwoAlertView.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/22.
//  Copyright © 2017年 YYKJ. All rights reserved.
//  两列弹框

#import "BVAddressAlertView.h"
#import "BVAllAlertTool.h"
#import "BVAddressModel.h"

#define AlertW [UIScreen mainScreen].bounds.size.width-80

@interface BVAddressAlertView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic ,retain) UIView *alertView;

@property (nonatomic ,strong) BVAllAlertTool     *mineTool;

@property (nonatomic ,strong) UIView             *upLine;
@property (nonatomic ,strong) UIButton           *upLeftBtn;
@property (nonatomic ,strong) UIButton           *upRightBtn;
@property (nonatomic ,strong) UIPickerView       *pickView;

@property (nonatomic, strong) NSMutableArray     *cityArr;
@property (nonatomic, strong) NSMutableArray     *countyArr;

@end

@implementation BVAddressAlertView

- (instancetype)init{
    
    if (self = [super init]) {
        
        [self setFrame:[UIScreen mainScreen].bounds];
        [self setBackgroundColor:[UIColor BVCoverColor]];
        
        self.alertView = [[UIView alloc]init];
        [self addSubview:self.alertView];
        [self.alertView setBackgroundColor:[UIColor whiteColor]];
        [self.alertView setFrame:CGRectMake(0, 0, AlertW, 200)];
        [self.alertView.layer setCornerRadius:5.0];
        [self.alertView.layer setPosition:self.center];
        
        self.upLeftBtn = [self upBtnWithTitle:@"取消" withTag:100];
        [self.upLeftBtn setFrame:CGRectMake(10, 10, 50, 25)];
        
        self.upRightBtn = [self upBtnWithTitle:@"确认" withTag:200];
        [self.upRightBtn setFrame:CGRectMake(AlertW-10-50, 10, 50, 25)];
        
        self.upLine = [[UIView alloc]initWithFrame:CGRectMake(0,40,AlertW,1.0)];
        [self.upLine setBackgroundColor:[UIColor BVLineGrayColor]];
        [self.alertView addSubview:self.upLine];
        
        self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, self.alertView.width, self.alertView.height-60)];
        [self.alertView addSubview:self.pickView];
        [self.pickView setDataSource:self];
        [self.pickView setDelegate:self];
        
        [self fetchTimetData];
        
    }
    return self;
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        return self.cityArr.count;
    }else{
        return self.countyArr.count;
    }
}

#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if (component == 0) {
        BVAddressModel *province = self.cityArr[row];
        return province.name;
    }else{
        BVAddressModel *model = self.countyArr[row];
        return model.name;
    }
}

#pragma mark - 监听pick的选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        BVAddressModel *model = self.cityArr[row];
        _countyArr = model.cities;
        
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        
    }
}

#pragma mark - 弹出
-(void)showMKPAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

-(void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
    
}

-(void)disBtnClick:(UIButton *)btn{
    if (btn.tag == 100) {
        [self removeFromSuperview];
    }else{
        BVAddressModel *shi = self.cityArr[[self.pickView selectedRowInComponent:0]];
        BVAddressModel *qu = self.countyArr[[self.pickView selectedRowInComponent:1]];
        NSString *str = [NSString stringWithFormat:@"%@-%@",shi.name,qu.name];
        if (self.AlertResult) {
            self.AlertResult(str);
        }
        [self removeFromSuperview];
    }
    
}

- (void)fetchTimetData {
    
    [self.mineTool fetchAddressCity:^(NSMutableArray *city) {
        [self.cityArr addObjectsFromArray:city];
        BVAddressModel *model = self.cityArr[0];
        [self.countyArr addObjectsFromArray:model.cities];
    } failure:^(NSError *error) {
        
    }];
    
}

-(BVAllAlertTool *)mineTool{
    if (!_mineTool) {
        _mineTool = [[BVAllAlertTool alloc]init];
    }
    return _mineTool;
}

-(NSMutableArray *)cityArr{
    if (!_cityArr) {
        self.cityArr = [[NSMutableArray alloc]init];
    }
    return _cityArr;
}

-(NSMutableArray *)countyArr{
    if (!_countyArr) {
        self.countyArr = [[NSMutableArray alloc]init];
    }
    return _countyArr;
}

-(UIButton *)upBtnWithTitle:(NSString *)title withTag:(NSInteger )tag{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.alertView addSubview:btn];
    [btn addTarget:self action:@selector(disBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn setTitleColor:[UIColor BVBaseOrangeColor]forState:UIControlStateNormal];
    [btn.layer setCornerRadius:3.0];
    [btn.layer setMasksToBounds:YES];
    [btn.layer setBorderWidth:1.0];
    [btn.layer setBorderColor:[UIColor BVLineGrayColor].CGColor];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTag:tag];
    
    return btn;
}


@end
